'use strict';
const axios = require('axios');
const cheerio = require('cheerio');
const db = uniCloud.database();
const collection = db.collection('protraitList');



exports.main = async (event, context) => {
	//event为客户端上传的参数;
	let nowPn = 0;
	// let keyList = ['空间素材','素材'];
	let keyList = ['搞笑表情','qq表情','微信表情','动漫图片','头像','情侣头像', '女生头像','动漫头像','男生头像','男头', '女头', '情头','表情包','表情','壁纸','手机壁纸','qq头像','斗图','女图','大图','美图','图片','空间素材'];
	let keyObj = {
		qq头像:'qq头像',
		头像:'头像',
		情侣头像:'情头',
		女生头像:'女头',
		动漫头像:'动漫头像',
		男生头像:'男头',
		男头:'男头',
		女头:'女头',
		情头:'情头',
		表情包:'表情包',
		搞笑表情:'表情包',
		qq表情:'表情包',
		微信表情:'表情包',
		斗图:'表情包',
		壁纸:'壁纸',
		手机壁纸:'壁纸',
		女图:'女生图片',
		大图:'大图',
		美图:'美图',
		图片:'图片',
		动漫图片:'动漫图片'
	}
	let keyIndex = 0;
	let saveList = [];
	let checkObj = {};
	//返回数据给客户端
	let getList = async () => {
		let tempUrl =
			`https://tieba.baidu.com/f?kw=${encodeURI(keyList[keyIndex])}&ie=utf-8&pn=${nowPn}`;
		console.log('tempUrl', tempUrl);
		// let tempRes = await axios.get(tempUrl);
		
		let tempRes = await axios({
			headers: {
				'Cookie': '__yjs_duid=1_7ad299caae06f8581dd9d55a111edfb11627263500335; BIDUPSID=61C51347AE79090FCBC917C87FBD024C; PSTM=1627269795; BAIDUID=9AE68FB1EB539AC6A54D3AA71056FBD8:FG=1; bdshare_firstime=1627383519189; H_PS_PSSID=34299_34380_31254_34331_34004_34092_34094_26350; delPer=0; PSINO=5; BDRCVFR[x4e6higC8W6]=mk3SLVN4HKm; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; tb_as_data=85556809faa70021334221d5495c2f6b96ff40cd1789fc0c64c2a7970f788e3d354112bbcf4840cb8aca24a1f7651f9c204a459617443a0decad214be0b9771c1ed8b25dd0a97daeac2be41e174465aed1115a4919f5a47d622c1237bb5be564; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1627383513,1627436813,1627437294,1627987353; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1627987353; video_bubble0=1',
				'Referer': tempUrl,
				'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0',
			},
			method: 'get',
			url: tempUrl
		})
		
		const $ = cheerio.load(tempRes.data, {
			normalizeWhitespace: true,
			decodeEntities: false
		});
		let contentList = $('.t_con');
		$('.t_con').each(async (index, elem) => {
			let tempNum = Number($(elem).children('.col2_left').text());
			if (tempNum > 200) {
				let detailItem = $($(elem).find('.j_th_tit a'));
				let tempItem = {
					num: tempNum,
					title: detailItem.attr('title'),
					url: detailItem.attr('href'),
					type: keyObj[keyList[keyIndex]] ? keyObj[keyList[keyIndex]] : keyList[keyIndex],
					hasInit: false
				}
				let checkCountInSql = await collection.where({
					url: tempItem['url']
				}).count();
				console.log('checkCountInSql', checkCountInSql);
				if (checkCountInSql['total'] === 0 && tempItem['title'] && !checkObj[
						tempItem['url']] && checkTitle(tempItem.title)) {
					saveList.push(tempItem);
					checkObj[tempItem['url']] = true;
				}
			}

		})
		nowPn += 50;
		
		await new Promise(function(resolve) {
		    setTimeout(resolve, 2000)
		});
		
		
		if (nowPn <= 200) await getList();
		else {
			console.log(`获取${keyList[keyIndex]}结束`);
			if (saveList.length > 0) {
				let res = await collection.add(saveList);
				console.log('saveToSql', res);
			}
			saveList = [];
			keyIndex++;
			if (keyList.length > keyIndex) {
				nowPn = 0;
				await getList();
			} else {
				console.log('全部获取结束');
			}
		}
	}
	await getList();
	return event;
};

function checkTitle(title) {
	let forbidTitleList = ['水贴', '吧务', '联系','官方','经验','删除'];
	for (let i = 0; i < forbidTitleList.length; i++) {
		if (title.indexOf(forbidTitleList[i]) != -1) return false;
	}
	return true;
}
